package cmsapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	"time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	productissues struct {
		ID 				 bson.ObjectId `json:"_id" bson:"_id"`
		ProductID 				string `json:"productid" bson:"productid"`
		IssuesDesc 				string `json:"issuesdesc" bson:"issuesdesc"`
		AssignResolver 			string `json:"assignresolver" bson:"assignresolver"`
		ExpectSettlementDate time.Time `json:"expectsettlementdate" bson:"expectsettlementdate"`
		CounterMeasures 		string `json:"countermeasures" bson:"countermeasures"`
		WhetherSolve 			string `json:"whethersolve" bson:"whethersolve"`
		Creater					string `json:"creater" bson:"creater"`
		CreaterDate			 time.Time `json:"createrdate" bson:"createrdate"`
		Title					string `json:"title" bson:"title"`
	}
	insertproductissues struct {
		ProductID 				string `json:"productid" bson:"productid"`
		IssuesDesc 				string `json:"issuesdesc" bson:"issuesdesc"`
		AssignResolver 			string `json:"assignresolver" bson:"assignresolver"`
		ExpectSettlementDate time.Time `json:"expectsettlementdate" bson:"expectsettlementdate"`
		CounterMeasures 		string `json:"countermeasures" bson:"countermeasures"`
		WhetherSolve 			string `json:"whethersolve" bson:"whethersolve"`
		Creater					string `json:"creater" bson:"creater"`
		CreaterDate			 time.Time `json:"createrdate" bson:"createrdate"`
		Title					string `json:"title" bson:"title"`
	}
)

func GetProductIssuesListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="products_issues"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]productissues{}
	query.All(&ps)
	
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
}

func GetProductIssuesInfoHandler(c echo.Context) error {
	log.Info("GetUserInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname="OxyCms"
	model.DB().Document="products_issues"
	query,err := model.DB().QueryOne(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p:= productissues{}
	query.One(&p)
	str,err:= json.Marshal(p)
	if err != nil{
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
		"data":string(str),
	})

	return nil
}

func AddProductIssuesHandler(c echo.Context) error {
	p:= &insertproductissues{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	model.DB().Dbname="OxyCms"
	model.DB().Document="products_issues"
	err:=model.DB().Insert(p)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func UpdateProductIssuesHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateProductIssuesHandler",id)
	
	p:= &insertproductissues{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	
	model.DB().Dbname="OxyCms"
	model.DB().Document="products_issues"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , p)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	// model.DB().Document="products"
	// selector := bson.M{"_id": bson.ObjectIdHex(p.ProductID)}
	// data := bson.M{"$set": bson.M{"productionteam": p.WhetherSolve}}
	// err = model.DB().Update(selector, data)
	// if err != nil{
	// 	log.Error("更新出错",err)
	// 	c.JSON(404, map[string]interface{}{
	// 		"msg":   "error",
	// 	})
	// 	return err
	// }

	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func DelProductIssuesHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelProductIssuesHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="products_issues"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}